package com.fr.crf;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;

/**
 * @author 付荣
 * @version 创建时间：2018年4月12日 下午6:35:48
 */
public class ProductName {
	private static TransportClient transportClient;
	static {
		try {
			Settings settings = Settings.settingsBuilder().put("cluster.name", "lmm_ultimate_search_fangzhen").put("client.transport.sniff", true).build();
			transportClient = TransportClient.builder().settings(settings).build();
			transportClient.addTransportAddresses(new InetSocketTransportAddress(InetAddress.getByName("10.200.2.65"), 9300));
		} catch (UnknownHostException e) {
		}
	}
	public static void main(String[] args) throws IOException {
		SearchRequestBuilder sr = transportClient.prepareSearch("suggest").setTypes("suggest").setRequestCache(true);
		sr.setScroll(new TimeValue(1000000000));
		sr.setSize(1000);
		sr.setFetchSource("name", null);
		sr.setQuery(QueryBuilders.termQuery("type", "PRODUCTNAME"));
		SearchResponse searchResponse = sr.get();
		File file = new File("E:\\nameFZ.txt");
		FileWriter fw = new FileWriter(file);
		while (searchResponse != null && searchResponse.getHits().getHits().length > 0) {
			for (SearchHit hit : searchResponse.getHits().getHits()) {
				String name = (String) hit.getSource().get("name");
				if (StringUtils.isNotBlank(name)) {
					name = name.replaceAll("\\pP|\\pS", "").replaceAll("\\s", "");
					fw.append(name + "\n");
				}
			}
			searchResponse = transportClient.prepareSearchScroll(searchResponse.getScrollId()).setScroll(new TimeValue(1000000000)).get();
		}
		fw.close();
	}
}
